<template>
  <div class="login-container">
    <el-form
      ref="loginFormRef"
      :model="formData"
      :rules="loginRules"
      class="login-form"
    >
      <div class="flex text-white items-center py-4 title-wrap">
        <span class="text-2xl flex-1 text-center title"> 客户注册 </span>
      </div>

      <el-form-item prop="userName">
        <div class="p-2 text-white">
          <svg-icon icon-class="user" />
        </div>
        <el-input
          ref="userName"
          v-model="formData.userName"
          class="flex-1"
          size="large"
          placeholder="用户名"
          name="userName"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="p-2 text-white">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          v-model="formData.password"
          class="flex-1"
          placeholder="密码"
          :type="passwordVisible === false ? 'password' : 'input'"
          size="large"
          name="password"
        />
        <span class="mr-2" @click="passwordVisible = !passwordVisible">
          <svg-icon
            :icon-class="passwordVisible === false ? 'eye' : 'eye-open'"
            class="text-white cursor-pointer"
          />
        </span>
      </el-form-item>
      <el-form-item prop="repassword">
        <span class="p-2 text-white">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          v-model="formData.repassword"
          class="flex-1"
          placeholder="确认密码"
          :type="passwordVisible === false ? 'password' : 'input'"
          size="large"
          name="repassword"
          @keyup.enter="handleSubmit"
        />
        <span class="mr-2" @click="passwordVisible = !passwordVisible">
          <svg-icon
            :icon-class="passwordVisible === false ? 'eye' : 'eye-open'"
            class="text-white cursor-pointer"
          />
        </span>
      </el-form-item>

      <el-button
        size="default"
        :loading="loading"
        type="primary"
        class="w-full"
        @click.prevent="handleSubmit"
      >
        注册
      </el-button>

      <!-- 账号密码提示 -->
      <div class="mt-4 text-white text-sm">
        欢迎使用佳境VR管理系统~<router-link to="/login">立即登录</router-link>
      </div>
    </el-form>
  </div>
</template>

<script setup lang="ts">
import router from "@/router"
import SvgIcon from "@/components/SvgIcon/index.vue"

// 状态管理依赖
import { useUserStore } from "@/store/modules/user"

// API依赖
import { LocationQuery, LocationQueryValue, useRoute } from "vue-router"
import { RegisterData } from "@/api/auth/types"

const userStore = useUserStore()
const route = useRoute()

/**
 * 按钮loading
 */
const loading = ref(false)
/**
 * 密码是否可见
 */
const passwordVisible = ref(false)

/**
 * 登录表单引用
 */
const loginFormRef = ref(ElForm)

const formData = ref<RegisterData>({
  userName: "",
  password: "",
  repassword: "",
})

const loginRules = reactive({
  userName: [
    {
      required: true,
      trigger: "blur",
      message: `请输入用户名`,
    },
  ],
  password: [
    {
      required: true,
      trigger: "blur",
      validator: passwordValidator,
    },
  ],
  repassword: [
    {
      required: true,
      trigger: "blur",
      validator: passwordValidator,
    },
  ],
})

/**
 * 密码校验器
 */
function passwordValidator(rule: any, value: any, callback: any) {
  if (!value) {
    callback(new Error("请输入密码"))
  }
  if (value.length < 6) {
    callback(new Error("密码不能少于6位"))
  } else {
    callback()
  }
}

/**
 * 注册
 */
function handleSubmit() {
  loginFormRef.value.validate((valid: boolean) => {
    if (valid) {
      const { password, repassword } = formData.value
      if (password !== repassword) {
        ElMessage.warning("两次密码输入不一致")
        return
      }
      loading.value = true
      userStore
        .register(formData.value)
        .then(() => {
          const query: LocationQuery = route.query
          const redirect = (query.redirect as LocationQueryValue) ?? "/"
          const otherQueryParams = Object.keys(query).reduce(
            (acc: any, cur: string) => {
              if (cur !== "redirect") {
                acc[cur] = query[cur]
              }
              return acc
            },
            {}
          )

          router.push({ path: redirect, query: otherQueryParams })
        })
        .finally(() => {
          loading.value = false
        })
    }
  })
}
</script>

<style lang="scss" scoped>
.login-container {
  width: 100%;
  min-height: 100%;
  overflow: hidden;
  background-color: #2d3a4b;

  .title-wrap {
    filter: contrast(30);

    .title {
      letter-spacing: 2px;
    }
  }

  .login-form {
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;

    .captcha {
      position: absolute;
      top: 0;
      right: 0;

      img {
        width: 120px;
        height: 48px;
        cursor: pointer;
      }
    }
  }
}

.el-form-item {
  background: rgb(0 0 0 / 10%);
  border: 1px solid rgb(255 255 255 / 10%);
  border-radius: 5px;
}

.el-input {
  background: transparent;

  // 子组件 scoped 无效，使用 :deep
  :deep(.el-input__wrapper) {
    padding: 0;
    background: transparent;
    box-shadow: none;

    .el-input__inner {
      color: #fff;
      caret-color: #fff;
      background: transparent;
      border: 0;
      border-radius: 0;

      &:-webkit-autofill {
        box-shadow: 0 0 0 1000px transparent inset !important;
        -webkit-text-fill-color: #fff !important;
      }

      // 设置输入框自动填充的延迟属性
      &:-webkit-autofill,
      &:-webkit-autofill:hover,
      &:-webkit-autofill:focus,
      &:-webkit-autofill:active {
        transition: color 99999s ease-out, background-color 99999s ease-out;
        transition-delay: 99999s;
      }
    }
  }
}
</style>
